Skip to content

Add support for multiple named OpenAI chat models in Spring Boot starter#162

Open
ghtjr410 wants to merge 1 commit intolangchain4j:mainfrom
ghtjr410:feature/4286-support-multiple-named-openai-models
Open

Add support for multiple named OpenAI chat models in Spring Boot starter#162
ghtjr410 wants to merge 1 commit intolangchain4j:mainfrom
ghtjr410:feature/4286-support-multiple-named-openai-models

Conversation

@ghtjr410
Copy link

@ghtjr410 ghtjr410 commented Jan 9, 2026

Issue

Closes langchain4j/langchain4j#4286

Change

Add support for multiple named OpenAI chat models in Spring Boot starter.

Configuration

# Global settings (inherited by all named models)
langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}

# Named models
langchain4j.open-ai.chat-model.fast.model-name=gpt-4.1-mini
langchain4j.open-ai.chat-model.smart.model-name=gpt-5.2

Usage

@AiService(wiringMode = EXPLICIT, chatModel = "openAiChatModelFast")
interface FastAssistant { }

@AiService(wiringMode = EXPLICIT, chatModel = "openAiChatModelSmart")
interface SmartAssistant { }

Implementation

  • Uses BeanDefinitionRegistryPostProcessor to dynamically register named model beans
  • Scans Environment for unknown property keys under chat-model.* prefix
  • Supports global property inheritance (api-key, base-url, etc.)
  • No changes to Properties.java - 100% backwards compatible
  • Named models support ChatModelListener callbacks and Spring RestClient integration

Bean Naming Convention

Configuration Bean Name
chat-model.fast.* openAiChatModelFast
chat-model.4-1-mini.* openAiChatModel41Mini

General checklist

  • There are no breaking changes
  • I have added unit and/or integration tests for my change
  • The tests cover both positive and negative cases
  • I have manually run all the unit and integration tests in the module I have added/changed, and they are all green
  • I have added/updated the documentation
  • I have added an example in the examples repo (only for "big" features)

Checklist for adding new Spring Boot starter

N/A - This PR modifies an existing starter.

@ghtjr410 ghtjr410 force-pushed the feature/4286-support-multiple-named-openai-models branch from 53db89b to 5a0dbf9 Compare January 9, 2026 00:46
@ghtjr410 ghtjr410 marked this pull request as ready for review January 9, 2026 04:10
@dliubarskyi
Copy link
Member

@ThomasVitale I would appreciate a review from you, if you have some spare time 🙏

Copy link

@Tcharl Tcharl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really interested in that feature personnally

private static final String STREAMING_CHAT_MODEL_PREFIX = PREFIX + ".streaming-chat-model";

private static final Set<String> KNOWN_PROPERTIES = Set.of(
"base-url", "api-key", "organization-id", "project-id", "model-name",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strong typing and specific class would have been better IMHO

@ThomasVitale
Copy link
Collaborator

@dliubarskyi I somehow didn't see your tag until now, sorry for the late answer. Spring Framework 7/Spring Boot 4 introduced the BeanRegistrar API for solving the problem of auto-configuring multiple beans of the same type. Here's an example. More details: https://docs.spring.io/spring-framework/reference/core/beans/java/programmatic-bean-registration.html

Based on the decision on how to introduce support for Spring Boot 4 in LangChain4j, you might consider introducing support for multiple chat models only in the Spring Boot 4 variant using the BeanRegistrar API. If really needed, the Spring Boot 3 variant could use BeanDefinitionRegistryPostProcessor.

@dliubarskyi
Copy link
Member

@ThomasVitale thanks a lot for the insights!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Support multiple OpenAI models in the spring boot starter

4 participants